/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package au.edu.swin.unilearn.assignment;

import au.edu.swin.unilearn.subject.*;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author DidierLeung
 */
@Stateless
public class AssignmentSessionBean implements AssignmentSessionRemote
{
    @PersistenceContext
    private EntityManager em;
    
    public void addAssignment(Assignment a)
    {
        AssignmentEntity assEntity = new AssignmentEntity();
        assEntity.setAssignmentData(a);
        SubjectEntity se = em.find(SubjectEntity.class, (a.getSubjectObj()).getSubject_code());
        assEntity.setSubjectEntity(se);
        em.persist(assEntity);
    }

    public List<Assignment> getAllAssignments()
    {
        List<Assignment> results = new ArrayList<Assignment>();
        Query allAssignmentQuery = em.createNamedQuery("findAllAssignments");
        List dbResults = allAssignmentQuery.getResultList();
        for (int i = 0; i < dbResults.size(); i++)
        {
            AssignmentEntity assEntity = (AssignmentEntity) dbResults.get(i);
            results.add(assEntity.getAssignmentData());
        }
        return results;
    }

    public Assignment getAssignment(String assignmentNo, String subjectCode)
    {
        AssignmentPK pk = new AssignmentPK(assignmentNo, subjectCode);
        AssignmentEntity assEntity = em.find(AssignmentEntity.class, pk);
        return assEntity.getAssignmentData();
    }

    public void updateAssignment(Assignment a)
    {
        AssignmentPK pk = new AssignmentPK(a.getAssignment_no(), a.getSubjectObj().getSubject_code());
        AssignmentEntity ae = em.find(AssignmentEntity.class, pk);
        ae.setExtendedDeadLine(a.getExtendedDeadLine());
        em.persist(ae);
    }
    
    public List<Assignment> getAssignments(String subjectCode)
    {
//        System.out.println("Get Assignment for subject > AssignmentSessionBean");
        List<Assignment> results = new ArrayList<Assignment>();
        Query allAssignmentQuery = em.createNamedQuery(
                        "findAllAssignmentForSubjectCode") .setParameter("code", subjectCode);
        List dbResults = allAssignmentQuery.getResultList();
        for (int i = 0; i < dbResults.size(); i++)
        {
            AssignmentEntity assEntity = (AssignmentEntity) dbResults.get(i);
            results.add(assEntity.getAssignmentData());
        }
        return results;
    }
    
    public List<Assignment> getAllAssignmentForMarking(String subjectCode)
    {
        List<Assignment> results = new ArrayList<Assignment>();
        Query allAssignmentQuery = em.createNamedQuery(
                        "findAllAssignmentForMarking") .setParameter("code", subjectCode);
        List dbResults = allAssignmentQuery.getResultList();
        for (int i = 0; i < dbResults.size(); i++)
        {
            AssignmentEntity assEntity = (AssignmentEntity) dbResults.get(i);
            results.add(assEntity.getAssignmentData());
        }
        return results;
    }
}
